<template>
	<view class="login">
		<view class="bg">
			<view class="big-box">
				<view class="logo">
					<image src="../../static/logo.png" mode="widthFix"></image>
				</view>
				<view class="title">
					轻 阅
				</view>

				<view class="list">
					<text>账号：</text>
					<input type="text" placeholder="请输入您的账号" placeholder-style="color: #9c9c9c" v-model="phone">
				</view>
				<view class="list">
					<text>密码：</text>
					<input type="password" placeholder="请输入您的密码" placeholder-style="color: #9c9c9c" v-model="password">
				</view>

				<view class="btn-box">
					<view class="btn" @click="getLogin()">
						登录账号
					</view>
					<view class="btn" @click="goReg()">
						去注册
					</view>
				</view>

			</view>
		</view>
		<view class="bottom">

		</view>
	</view>
</template>

<script>
	export default {
		name: 'login',
		data() {
			return {
				phone: '',
				password: '',
			};
		},
		methods: {
			goReg() {
				uni.navigateTo({
					url: '../register/register'
				})
			},
			async getLogin() {
				if (this.phone.length == 0 || this.password.length == 0) {
					uni.showToast({
						title: '账号或密码不能为空',
						icon: 'none'
					})
					return
				} else {
					await uniCloud.callFunction({
						name: 'getUser',
						data: {
							phone: this.phone
						}
					}).then(res => {
						console.log('res ==> ', res);
						if (res.result.data.length == 0) {
							uni.showToast({
								title: '账号不存在',
								icon: 'none'
							})
						} else {
							if (this.phone === res.result.data[0].phone && this.password === res.result.data[0]
								.password) {
								uni.showToast({
									title: '登录成功,即将跳转...',
									icon: 'none'
								})
								let obj = {
									nickName: res.result.data[0].nickName,
									phone: res.result.data[0].phone,
									password: res.result.data[0].password
								};
								uni.setStorageSync('userInfo', JSON.stringify(obj))
								setInterval(() => {
									uni.switchTab({
										url: '../my/my'
									})
								}, 2000)
							} else {
								uni.showToast({
									title: '密码错误',
									icon: 'none'
								})
							}
						}
					}).catch(err => {
						console.log('err ==> ', err);
					})
				}

			}
		},
	}
</script>

<style lang="scss">
	.bg {
		width: 100%;
		// height: 1234rpx;
		height: 100vh;
		background-image: url('../../static/background-image.png');
		background-repeat: no-repeat;
	}

	.bottom {
		width: 100%;
		height: 80rpx;
		background-color: #FFE5B4;
	}

	.big-box {
		width: 680rpx;
		height: 1100rpx;
		background-color: #FCECCC;
		margin: 0 auto;
		position: absolute;
		top: 60px;
		left: 18px;
		border-radius: 40rpx;
		border: 1px solid #FC768A;

		.logo {
			width: 160rpx;
			height: 160rpx;
			margin: 0 auto;
			margin-top: 60rpx;

			image {
				width: 100%;
				display: block;
			}
		}

		.title {
			text-align: center;
			font-size: 60rpx;
			margin-top: 20rpx;
			color: #FCB271;
		}

		.list {
			display: flex;
			align-items: center;
			width: 600rpx;
			height: 80rpx;
			line-height: 80rpx;
			border-bottom: 1px solid #f5841a;
			margin: 50rpx auto;
		}

		.btn-box {
			display: flex;
			justify-content: space-evenly;

			.btn {
				margin-top: 20rpx;
				width: 200rpx;
				height: 80rpx;
				background-color: #F58519;
				border-radius: 50rpx;
				color: white;
				text-align: center;
				line-height: 80rpx;
				font-size: 28rpx;
			}
		}
	}
</style>